package com.gongpingjialoan.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.NewUseCarAssResultActivity;
import com.gongpingjialoan.adapter.car.CarlistSelectFilterAdapter;
import com.gongpingjialoan.bean.SelectBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/1.
 */
public class VipBuyCarDialog extends Dialog implements AdapterView.OnItemClickListener{

    private String price = "0";

    private Button mButton;

    private NewUseCarAssResultActivity mNewUseCarAssResultActivity;



    //车龄   (多选)
    private String[] show_age = {"1年内", "2年内", "3年内", "4年内", "5年内", "车龄不限"};
    private String[] need_age = {"1", "2", "3", "4", "5", "车龄不限"};

    //里程   (多选)
    private String[] show_mile = {"1-5万km", "5-10万km", "10万km以上"};
    private String[] need_mile = {"1-5", "5-10", "10-"};



    private MaterialOneSlider mMaterialOneSlider;

    private GridView  ageGridView, mileGridView;

    private CarlistSelectFilterAdapter  adapter_age, adapter_mile;

    private List<SelectBean> list_age, list_mile;

    private String str_age = "", str_mile = "";

    private String mPrice;

    public VipBuyCarDialog(Context context , String price) {
        super(context, R.style.dialogs);
        mPrice = price;
        mNewUseCarAssResultActivity = (NewUseCarAssResultActivity) context;
//        padding = DhUtil.dip2px(context, 20);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eval_dialog);
        mMaterialOneSlider = (MaterialOneSlider) findViewById(R.id.slider);
        mMaterialOneSlider.haveBrand = true;
        try {
            mMaterialOneSlider.setMax((int) Float.parseFloat(mPrice) + 5);
        } catch (NumberFormatException e) {
            mMaterialOneSlider.setMax(1000);
        }
        mMaterialOneSlider.setRangeSliderListener(new MaterialOneSlider.RangeOneListener() {
            @Override
            public void onMaxChanged(int newValue) {
            }

            @Override
            public void onMinChanged(int newValue) {
                price = newValue + "";
                setButton();
            }
        });


        ageGridView = (GridView) findViewById(R.id.age);
        ageGridView.setSelector(getContext().getResources().getDrawable(R.drawable.trans));
        adapter_age = new CarlistSelectFilterAdapter(getContext());
        ageGridView.setOnItemClickListener(this);
        ageGridView.setAdapter(adapter_age);

        mileGridView = (GridView) findViewById(R.id.mile);
        mileGridView.setSelector(getContext().getResources().getDrawable(R.drawable.trans));
        adapter_mile = new CarlistSelectFilterAdapter(getContext());
        mileGridView.setOnItemClickListener(this);
        mileGridView.setAdapter(adapter_mile);


        list_age = new ArrayList<SelectBean>();
        for (int i = 0; i < show_age.length; i++) {
            SelectBean bean = new SelectBean(show_age[i], need_age[i], false);
            list_age.add(bean);
        }
        adapter_age.setData(list_age);

        list_mile = new ArrayList<SelectBean>();
        for (int i = 0; i < show_mile.length; i++) {
            SelectBean bean = new SelectBean(show_mile[i], need_mile[i], false);
            list_mile.add(bean);
        }
        adapter_mile.setData(list_mile);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mMaterialOneSlider.isMakeSure = false;
            }
        }, 500);


        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mNewUseCarAssResultActivity.gotoVip(price, str_age , str_mile);
            }
        });
    }

    //重置单选 (设置为false)
    private void resetRadio(List<SelectBean> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCheack(false);
        }
    }

    private void  setButton(){
        if(TextUtils.isEmpty(str_age) ||TextUtils.isEmpty(str_mile) || "0".equals(price)){
            mButton.setBackgroundResource(R.drawable.button_no_click);
            mButton.setEnabled(false);
        } else {
            mButton.setBackgroundResource(R.drawable.button_big_bg);
            mButton.setEnabled(true);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == ageGridView) {
            if (list_age.get(position).isCheack()) {
                resetRadio(list_age);
                str_age = "";
            } else {
                resetRadio(list_age);
                list_age.get(position).setCheack(true);
                str_age = list_age.get(position).getNeed_name();
            }
            adapter_age.setData(list_age);
            setButton();
        } else if (parent == mileGridView) {
            if (list_mile.get(position).isCheack()) {
                resetRadio(list_mile);
                str_mile = "";
            } else {
                resetRadio(list_mile);
                list_mile.get(position).setCheack(true);
                str_mile = list_mile.get(position).getNeed_name();
            }
            adapter_mile.setData(list_mile);
            setButton();
        }
    }
}
